Ext.BLANK_IMAGE_URL = '/extjs/resources/images/default/s.gif';
Ufida.widgets.Menu = function(config) {
	Ufida.widgets.self=this;
	Ufida.widgets.Menu.superclass.constructor.call(this, {
				id : Ext.id(),
				height : 110
			});
	Ext.apply(this, config);
};

//获取服务列表
var servieList = [];
/**
var servieArray = services.split(";");
for(var i=0 ;i < servieArray.length-1&&i<2;i++){
	servieList.push({
		text : servieArray[i].split(',')[0],
		href : servieArray[i].split(',')[1]
	})
}
*/
Ufida.widgets.Menu.template = new Ext.XTemplate(
		'<div>',
		// top
		'<table cellpadding="0" cellspacing="0" class="x-default-top-head">',
		'<tr>',
		'<td width="33%" rowspan="2" style="padding-left:15px;">',
		'<a href="http://www.wecoo.com" alt="伟库网"><img src="/images/logo.gif" /></a>',
		'</td>',
		'	<td width="67%" align="right" class="x-default-top-head-right">',
		'<span class="logo_menu">',
		'<ul>',
		'<li class="logomenu02" id = "oncss" style="white-space:nowrap;">',
		'<span id="" style="font-family:Arial; font-size:12px; line-height:2em; color:#666666;">欢迎您 </span>',
		'<span id="username" style="font-family:Arial; font-size:12px; line-height:2em; padding-right:8px;"></span>',
		'<span id="" style="font-family:Arial; font-size:12px; line-height:2em; color:#666666; padding-right:2px;">当前企业:</span>',
		'<span id="accountbookname"  style="font-size:12px; font-family:Arial; line-height:2em; color:#666666; padding-right:8px;"></span>' +
		'<span id="" style="font-family:Arial; font-size:12px; line-height:2em; color:#666666; padding-right:2px;">当前期间:</span>',
		'<span id="currentPeriod" style="font-family:Arial; font-size:12px; line-height:2em; color:#666666; padding-right:15px;"></span>',
		'</li>',
		'<li class="logomenu04" class="navtop">',
		'<a  id="myService" href="javascript:doLogoff();" class="navtop" style="line-height:2em;white-space:nowrap;">我的工作台</a>',
		'</li>',
		'<li class="logomenu04" class="navtop" style="background:url(/images/gif_0922.gif) no-repeat 0px 9px; padding-left:18px;">',
//		'<a target="_blank" href="#" onclick="openDoyooCustomerChat(6572); return false;" class="navtop" style="line-height:2em;white-space:nowrap;">在线客服</a>',
		'<a target="_blank" href="http://www.wecoo.com/clientservice/" class="navtop" style="line-height:2em;white-space:nowrap;">在线客服</a>',
		'</li>',
		'<li class="logomenu03">',
		'<a href="/Help/FinanceHelp.html" target="_blank" class="navtop" style="line-height:2em;white-space:nowrap;">帮助</a>',
		'	</li>',
		'<li class="logomenu03">',
		'<span><a href="javascript:doLogoff(\'login\');" class="navtop" style="line-height:2em;">退出</a></span>',
		'	</li>',
		'</ul></span>',
		'<td><input type="hidden" name="hiddenButton" id="hiddenButton" ></td>',
		'</tr>',
		// services
		'<tr class="Cservice"><td colspan="4"><ul>',
		' <li  style="float:right; padding:0x 10px 0 0; line-height:180%;" id="subMenu"></li>',
		'<tpl for="servieList">',
		'<li style="float:right; padding:0 8px 0 0; line-height:180%;"><a href="{href}">{text}</a></li>',
		'</tpl>',
		'<li id="goToOther" style="float:right; padding:0 5px 0 10px; color:#FF9900; line-height:180%;">快速切换服务入口:</li>',
		'<li id="imageAuto">',
		'</li>',
		'</ul></td></tr>',
		'</table>',
		// menu
		'<div id="menu_div_id"><ul>',
		'<tpl for="menus">',
		'<li id="td_{id}" style="display: inline;"><a href="{href}" id="{id}" name="menu">{name}</a></li>',
		'</tpl>',
		'</ul></div>',
		// message
		'<div id="toolbar" class="toolbar">',
		'<div id="top_head_text" class="left">用友在线服务</div>',
		'<div id="top_head_msg" class="center"></div>',
		'<div class="right">',
		'<a id="togglehelp" style="cursor:pointer" herf="javascript:void(0)">页面求助</a>',
		'&nbsp;<a style="cursor:pointer" href="javascript:window.history.back();">返回</a>',
		'</div></div>', '</div>')

Ext.extend(Ufida.widgets.Menu, Ext.BoxComponent, {
	currentclickid : null,
	datas : {
		menus : [{
					id : 'buy',
					name : '采购管理',
					href : '/accountMgr/welcome.html',
					items : [{
								text : '采购管理导航页',
								href : '/accountMgr/welcome.html'
							}, {
								text : '采购入库',
								href : '/purchase/warehousing.html'
							}, {
								text : '采购入库单',
								href : '/purchase/warehousingList.html'
							}, {
								hidden : 'true',
								text : '采购单据列表',
								items : [ {
									code : 'S04C008',
									hidden : 'true',
									text : '采购入库单',
									href : '/scmMng/purchase/purchaseOrder.html'
								},{
									code : 'S04C007',
									hidden : 'true',
									text : '采购订单',
									href : '/scmMng/purchase/orderFormList.html'
								}, {
									code : 'S04C009',
									hidden : 'true',
									text : '采购退货单',
									href :'/scmMng/purchase/purchaseBack.html'
								}, {
									code : 'S04C010',
									hidden : 'true',
									text : '付款单',
									href : '/accountMgr/cash/scmpaybill.html'
								}]
							}
							]
				}, {
					id : 'base',
					name : '基础设置',
					href : '/config/welcome.html',
					items : [{
								text : '基础设置导航页',
								href : '/config/welcome.html'
							}, {
								text : '供应商',
								href : '/base/supplier.html'
							},{
								text : '计量单位',
								href : '/base/measure.html'
							}, {
								text : '员工档案',
								href : '/base/employee.html'
							}, {
								text : '货品档案',
								href : '/base/stock.html'
							}, {
								text : '仓库档案',
								href : '/base/warehouse.html'
							}, {
								text : '颜色档案',
								href : '/base/color.html'
							}, {
								text : '产品档案',
								href : '/base/product.html'
							}, {
								text : '录入期初余额',
								items : [{
									code: 'S06C020',
									text : '录入科目期初余额',
									iconCls : '',
									href : '/accountMgr/sys/initBalanceBegin.html'
								},{
									code: 'S06C024',
									text : '录入期初现金流量',
									iconCls : '',
									href : '/accountMgr/sys/cashFlowInitBalance.html'
								}]
							}, {
								text : '辅助信息',
								items : [{
											code: 'S06C008',
											text : '凭证模板',
											href : '/accountMgr/base/voucherTpl.html'
										}, {
											code: 'S06C009',
											text : '凭证摘要',
											href : '/accountMgr/base/summary.html'
										}, {
											code: 'S06C010',
											text : '凭证字号',
											href : '/accountMgr/base/category.html'
										}, {
											code: 'S06C026',
											text : '结算方式',
											href : '/accountMgr/base/clearing.html'
										}, {
											code: 'S06C031',
											text : '计量单位',
											href : '/scmMng/base/measurement.html'
										}]
							}, {
								code: 'S06C021',
								hidden : 'true',
								text : '系统选项',
								iconCls : '',
								href : '/accountMgr/sys/accountPara.html'
							}, {
								code: 'S06C022',
								hidden : 'true',
								text : '权限分工',
								iconCls : '',
								href : '/accountMgr/base/permission.html'
							}, {
								code: 'S062222',
								text : '数据恢复',
								href : '/accountMgr/sys/bakBook.html'
							}]
				}, {
					id : 'data',
					name : '查询与报表',
					href : '/accountMgr/welcome_data.html',
					items : [{
								text : '查询与报表导航页',
								href : '/accountMgr/welcome_data.html'
							}, {
								text : '会计账簿',
								items : [{
									code: 'S01C005',
									text : '总账',
									iconCls : '',
									href : '/accountMgr/ledger/totalLedger.html'
								}, {
									code: 'S01C007',
									text : '明细账',
									iconCls : '',
									href : '/accountMgr/ledger/subLedger.html'
								}, {
									code: 'S01C018',
									text : '多栏明细账',
									href : '/accountMgr/ledger/multiColDetail.html'
								}, {
									code: 'S01C006',
									text : '科目余额表',
									iconCls : '',
									href : '/accountMgr/ledger/subjectBalance.html'
								}, {
									code: 'S01C014',
									hidden : 'true',
									text : '现金流量明细表',
									href : '/accountMgr/reportforms/cashFlowDetail.html'
								}]
							}, {
								text : '会计报表',
								items : [{
									code: 'S01C008',
									text : '资产负债表',
									iconCls : '',
									href : '/accountMgr/reportforms/substance.html'
								}, {
									code: 'S01C013',
									text : '应交增值税明细表',
									iconCls : '',
									href : '/accountMgr/reportforms/valueAddedTax.html'
								}, {
									code: 'S01C009',
									text : '利润表',
									iconCls : '',
									href : '/accountMgr/reportforms/gaintable.html'
								}, {
									code: 'S01C015',
									hidden : 'true',
									text : '现金流量表',
									href : '/accountMgr/reportforms/cashFlow.html'
								}]
							}, {
								text : '项目账',
								items : [{
											code: 'S01C012',
											text : '项目余额表',
											href : '/accountMgr/item/itemList.html'
										}, {
											code: 'S01C010',
											text : '项目总账',
											href : '/accountMgr/item/itemLedger.html'
										}, {
											code: 'S01C011',
											text : '项目明细账',
											href : '/accountMgr/item/itemDetail.html'
										}]
							},{
								text : '收支报表',
								items : [{
									code: 'S01C016',
									text : '月度收支明细表',
									href : '/accountMgr/reportforms/incomeDetail.html'
								},{
									code: 'S01C017',
									text : '月度收支汇总表',
									href : '/accountMgr/reportforms/incomeTot.html'
								}]
							}]
				}]
	},
	initComponent : function() {
		Ufida.widgets.Menu.superclass.initComponent.call(this)
	},
	onRender : function(ct, position) {
		
		
		if (!this.template) {
			this.template = Ufida.widgets.Menu.template
		}
		this.el = this.template.append(ct, this.datas, true)

		this.menus = {}

		Ext.each(this.datas.menus, function(def, e) {
					var m = Ext.get(def.id);

					m.on('click', function(e) {
								// if (this.menus[m.id]) //屏蔽掉该判断，即对所有一级
								// 菜单事件进行处理。否则，一级菜单中如果包含二级目录将不走此方法
								this.menu_mouse_click(def, e)
							}, this)

					// register mouseover event for menu element
					m.on('mouseover', function(e) {
								if (this.menus[m.id]) {
									this.menus[m.id].showAt([m.getX(),
											m.getY() + 20])
								}
							}, this)

					// create menu recursively
					if (def.items && def.items.length != 0) {
						// create menu
						var menu = this.create_menu(def)
						menu.on('mouseout', this.menu_cursor_move)
						this.menus[m.id] = menu

					}
				}, this);
		// 改变头部标题
		AN='杰硕软件';
		bookname='';
		LN='杨之廷';
		currentPeriod='201009';
		var bookname = AN.length > 8 ? AN.substr(0,8)+'..' : AN;
		Ext.getDom('accountbookname').title = AN;
		this.change_value('top_head_text', window.document.title);
		this.change_value('accountbookname', bookname);
		this.change_value('username',LN);
		this.change_value('currentPeriod',currentPeriod);
		if(servieList.length == 0)
			this.change_value('goToOther', '');
			
		Ext.get('togglehelp').on('click', function() {
					this.togglehelp()
				}, this);
				
				
				Ext.each(this.datas.menus, function(def) {
			var m = Ext.get(def.id);
			var menuCom=new Array('data','base','account')

			m.on('click', function(e) {
				if (this.menus[m.id])
					this.menu_mouse_click(def)
			}, this)

			// register mouseover event for menu element
			m.on('mouseover', function(e) {
				if (this.menus[m.id]) {
					this.menus[m.id].showAt([m.getX(), m.getY() + 20])
						menuCom.remove(m.id);
						if(this.menus[menuCom[0]])
							this.menus[menuCom[0]].hide();
						if(this.menus[menuCom[1]])
							this.menus[menuCom[1]].hide();
				}
			}, this)

			// create menu recursively
					if (def.items && def.items.length != 0) {
						// create menu
						var menu = this.create_menu(def)
						menu.on('mouseout', this.menu_cursor_move)
						this.menus[m.id] = menu

					}
				}, this);
	    
//	    if(servieArray.length>3){
//	    	servieList=[];
//		 	for (var i = 2; i < servieArray.length-1; i++) {
//				servieList.push({
//					text : servieArray[i].split(',')[0],
//					href : servieArray[i].split(',')[1]
//				})
//			}
//	    var moreMenu=new  Ext.menu.Menu({
//	    	shadow : true,
//	        style : {filter:'alpha(opacity=100,Style=0)',border:'1px solid #A19F9F',width:200,'background-image':'none',background:'#EDEDED',padding:0},
//	    	items : servieList,
//	    	overCls:'x-more-menu'
//	    });
//
//	    var tb = new Ext.Toolbar({
//			    renderTo:'subMenu',
//			    style : {background:'none',borderStyle:'none','margin-top':'-2.3px'},
//			    width : 90,
//			    toolbarCls : 'x-toolbar x-noicon-toolbar ',
//			    border : false,
//			    items : {
//				     text:'更多 <image src="/images/menu_arrow.png"></image>',
//				     style:{ 
//			         	height : 15 ,
//	 					background :'none',
//	 					borderStyle :'none'  
// 					},
// 				menu:moreMenu
// 			}
//		});
//	  }		
	},
	create_menu : function(def) {
		var hidden = true
		def.id = def.id ||Ext.id()
		var menu = new Ext.menu.Menu({
			id : (def.id)+'_menu',
			text : def.text,
			href : def.href,
			icon : def.icon
		})

		// add menu item
		Ext.each(def.items, function(item) {
					if (true || !item.code || showItems.indexOf(item.code) != -1) {
						hidden = false
						var menu_item = this.create_menu_item(item)
						if (menu_item != null)
							menu.add(menu_item)
					}
				}, this)
		if (hidden) {
			return null;
		}
		return menu
	},
	create_menu_item : function(def) {
		var menu = null;
		if (def.items) {
			menu = this.create_menu(def)
			// 子菜单没有需要显示的项,则该菜单项不显示
			if (menu == null) {
				return null;
			}
		}
		def.id = def.id ||Ext.id()
		var item = new Ext.menu.Item({
			id : def.id,
			text : def.text,
			href : def.href,
			menu : menu
		})

		if (!def.handler) {
			item.on('click', this.menu_item_click, this, def)
		}
		//if (def.code == 'S01C002' && !isInitedScmmng() && !isInitedCash())// 如果没启用进销存,不显示确认自动凭证
			//return null;
		return item
	},
	menu_item_click : function(item, e) {
		if (item.code != 'S01C001' && Ext.get('hiddenButton').dom.value) {
			displayMessageBox('提示!',
					'目前本系统运行在离线状态下，请更改系统状态后再进行业务跳转!，如有离线操作数据，请注意及时提交，以免耽误您的正常业务流转!');
			e.stopEvent();
		}
	},
	menu_mouse_click : function(menu, e) {
		if (Ext.get('hiddenButton').dom.value) {
			displayMessageBox('提示!',
					'目前本系统运行在离线状态下，请更改系统状态后再进行业务跳转!，如有离线操作数据，请注意及时提交，以免耽误您的正常业务流转!');
			e.stopEvent();
		} else {
			this.change_menu_style(menu.id);
			// 加载左侧菜单和右侧主编辑区
			window.location.href = this.menus[menu.id]['href']
		}
	},
	menu_cursor_move : function(_menu, e, _item) {
		var xleft = _menu.getEl().getX();
		var xright = xleft + _menu.getEl().getWidth();
		var ytop = _menu.getEl().getY();
		var ybottom = ytop + _menu.getEl().getHeight();
		if (e.getPageX() < xleft + 10 || e.getPageX() > xright - 10
				|| e.getPageY() < ytop + 3 || e.getPageY() > ybottom - 10) {
			if (!_item || !_item.initialConfig || !_item.initialConfig.menu) {
				hidden_menu = _menu;
				setTimeout(function() {
							hidden_menu.hide();
						}, 200);
			}

		}
	},
	change_menu_style : function(objId) {
		if (objId != this.currentclickid) {
			document.getElementById('menu_div_id').className = "top_menu_div_"+ objId;
			document.getElementById('td_' + objId).className = "top_menu_li_mainpage";
			document.getElementById(objId).className = "menu_div_font_bold";
			document.getElementById('toolbar').className = "toolbar_" + objId;

			if (this.currentclickid) {
				var o = document.getElementById('td_' + this.currentclickid);
				o.className = o.className.replace('top_menu_li_mainpage', '');
				var p = document.getElementById(this.currentclickid);
				p.className = p.className.replace('menu_div_font_bold', '');
			}
			this.currentclickid = objId;

		}
	},
	change_value : function(id, value) {
		Ext.get(id).dom.innerHTML = value
	},

	togglehelp : function() {
		// 特定功能的帮助
		var arrIcon = Ext.query('.helpTip', window.document);
		var i;
		var icon;
		if ((arrIcon != null) && (arrIcon.length > 0)) {
			for (i = 0; i < arrIcon.length; i++) {
				icon = Ext.get(arrIcon[i]);
				if (icon.isVisible())
					icon.hide();
				else
					icon.show();
			}
		}

		// 右上角页面的帮助
		var tipvgrid = Ext.query('.tipvgrid', window.document);
		if ((tipvgrid != null) && (tipvgrid != undefined) && (tipvgrid != '')) {
			if (Ext.get(tipvgrid[0]).isVisible()) {
				// Ext.getDom(tipvgrid[0]).style.visibility = "hidden";
				Ext.get(tipvgrid[0]).hide();
			} else if (icon == null || icon.isVisible()) {
				Ext.get(tipvgrid[0]).show();
			}
		}
	},
	generateServiceList : function() {
		if (!Ext.getCmp('services')) {
			var menu = new Ext.menu.Menu({
				id : 'services',
				text : 'text',
				items : [],
				shadow:false,
				listeners : {
					'mouseout' : this.menu_cursor_move
				}
			})

			var ss = services.split(';');
			for (var i = 0; i < ss.length - 1; i++) {
				var ss_items = ss[i].split(',');
				var item = new Ext.menu.Item({
					id : 'ss_items_' + i,
					text : ss_items[0],
					href : ss_items[1],
					activeClass:'x-menu-item-active-ss',
					ctCls:'x-menu-ss',
					icon : '/images/service.gif'
				})
				menu.addItem(item);
			}
		}
		menu.show('changeService');

	},
	changeServices:function(href){
		if ((typeof(isOffline)== 'undefined') || !isOffline) {
			window.location.href = href;
		}else
		displayMessageBox('提示!',
					'目前本系统运行在离线状态下，请更改系统状态后再进行业务跳转!，如有离线操作数据，请注意及时提交，以免耽误您的正常业务流转!');
	}

})

function doLogoff(url) {
	window.location.href = "/loginAction.do?method=goawayOnline&url="+url;
}

